﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using Xenta.Api;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Operations
{
    /// <summary>
    /// Gets the account and all related profiles.
    /// </summary>
    public class GetAccountWithProfiles : OperateEntityByID<dynamic>,
        IApiOperation
    {
        #region Utilities

        protected override void Work(IUnitOfWork uow, out dynamic result)
        {
            result = null;

            var account = uow.Execute(new GetAccount
            {
                EntityID = EntityID
            }).Result;
            if(account == null) 
                return;

            result = new ExpandoObject();
            result.Account = account;
            result.FileOwner = uow.Execute(new GetFileOwnerByAccount
            {
                AccountID = account.EntityID
            }).Result;
            result.ForumMember = uow.Execute(new GetForumMemberByAccount
            {
                AccountID = account.EntityID
            }).Result;
            result.CatalogManager = uow.Execute(new GetCatalogManagerByAccount
            {
                AccountID = account.EntityID
            }).Result;
            result.PublicationAuthor = uow.Execute(new GetPublicationAuthorByAccount
            {
                AccountID = account.EntityID
            }).Result;
            result.Customer = uow.Execute(new GetCustomerByAccount
            {
                AccountID = account.EntityID
            }).Result;
            result.Seller = uow.Execute(new GetSellerByAccount
            {
                AccountID = account.EntityID
            }).Result;
        }

        public override object Disclose(DisclosureLevel level, IDictionary<String, String> options)
        {
            if(Result == null)
                return null;
            dynamic ret = new ExpandoObject();

            ret.Account = Result.Account.Disclose(level, options);
            ret.FileOwner = Result.FileOwner != null
                ? Result.FileOwner.Disclose(level - 1, options)
                : null;
            ret.ForumMember = Result.ForumMember != null
                ? Result.ForumMember.Disclose(level - 1, options)
                : null;
            ret.CatalogManager = Result.CatalogManager != null
                ? Result.CatalogManager.Disclose(level - 1, options)
                : null;
            ret.PublicationAuthor = Result.PublicationAuthor != null
                ? Result.PublicationAuthor.Disclose(level - 1, options)
                : null;
            ret.Customer = Result.Customer != null
                ? Result.Customer.Disclose(level - 1, options)
                : null;
            ret.Seller = Result.Seller != null
                ? Result.Seller.Disclose(level - 1, options)
                : null;

            return ret;
        }

        #endregion
    }

    /// <summary>
    /// Gets the account and all related profiles by account email.
    /// </summary>
    public class GetAccountWithProfilesByEmail : Operation<dynamic>,
        IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets or sets the account email.
        /// </summary>
        public string Email
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Work(IUnitOfWork uow, out dynamic result)
        {
            result = null;
            var account = uow.Execute(new GetAccountByEmail
            {
                Email = Email
            }).Result;
            if(account == null) 
                return;
            result = uow.Execute(new GetAccountWithProfiles
            {
                EntityID = account.EntityID
            }).Result;
        }

        #endregion
    }
}
